//
//  LoginViewModel.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/7.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class LoginViewModel {
    
    //定义属性
    var loginParam :CMDBLoginParam = CMDBLoginParam()
    
    lazy var userAccount : CMDBAccountBean = CMDBAccountBean()
    
}


extension LoginViewModel{
    
       
        
        func login(loginParam: CMDBLoginParam, handler: CMDBAccountHandler,error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler) throws {
            
            self.loginParam.phone    = "18553351967"
            
            self.loginParam.password = "chd7323716"
            
            let param = self.loginParam.keyValues
            
            guard loginParam.phone.validPhoneNumber() else {
                printLog("\(CMDBUserControllerExceptionCodeType.PhoneInvalid.rawValue)")
                
                throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PhoneInvalid)
            }
            guard loginParam.password.cmdb_length >= Password_Min_Length else {
                // 可以在异常中 toast
                printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
                
                throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
            }
            
            CMDBHttpManager.getInstance.request(loginParam.URLString(), param: param, success: { (sysTime, data, page) in
                
                let account: CMDBAccountBean = CMDBAccountBean.objectWithKeyValues(data as! [String: AnyObject]) as! CMDBAccountBean
                
                CMDBApplicationManager.getInstance.saveAccount(account)
                
                self.userAccount = account
                
                handler(account: account)
                
                print("acc")
                
                }, error: error, failure: faliure)
        }
    
    func userLoginn(loginParam: CMDBLoginParam, handler: CMDBAccountHandler,error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler) throws {
        
        self.loginParam.phone    = "18553351967"
        
        self.loginParam.password = "chd7323716"
        
        let param = self.loginParam.keyValues
        
        
        CMDBHttpManager.getInstance.request(loginParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            let account: CMDBAccountBean = CMDBAccountBean.objectWithKeyValues(data as! [String: AnyObject]) as! CMDBAccountBean
            
            CMDBApplicationManager.getInstance.saveAccount(account)
            
            self.userAccount = account
            
            handler(account: account)
            
            print("acc")
            
            }, error: error, failure: faliure)
    }
    
    func userLogin(loginParam: CMDBLoginParam,handler: CMDBAccountHandler,error: CMDBHttpErrorHandler, faliure: CMDBHttpFailureHandler,finishCallBack:()->()){
        
        self.loginParam.phone    = "18553351967"
        
        self.loginParam.password = "chd7323716"
        
        let param = self.loginParam.keyValues
        
        CMDBHttpManager.getInstance.request(loginParam.URLString(), param: param, success: { (sysTime, data, page) in
            
            let account: CMDBAccountBean = CMDBAccountBean.objectWithKeyValues(data as! [String: AnyObject]) as! CMDBAccountBean
            
            CMDBApplicationManager.getInstance.saveAccount(account)
            
            self.userAccount = account
            
            handler(account: account)
            
            print("acc")
            
            finishCallBack()
            
            }, error: error, failure: faliure)
        
        
    }

    
}




